//
//  ChapterCell.swift
//  Reader
//
//  Created by home on 2019/10/10.
//  Copyright © 2019 codeguesser. All rights reserved.
//

import UIKit
import SwiftyJSON
class ChapterCell: UITableViewCell {
    @IBOutlet weak var lineView: UIView!
    @IBOutlet weak var downloadButton: UIButton!
    @IBOutlet weak var processConstraint: NSLayoutConstraint!
    @IBOutlet weak var titleLabel: UILabel!
    var data:JSON?{
        didSet{
            if let d = data{
                titleLabel.text = d["title"].stringValue
                downloadButton.isHidden = d["text"].stringValue.isNotEmpty
                if let read = ReadModel.mr_findFirst(with: NSPredicate(format: "bid == '\(d["bid"].stringValue)' and cid == '\(d["id"].stringValue)'", argumentArray: nil)),CGFloat(read.total) > 0{
                    processConstraint.constant = UIScreen.main.width * CGFloat(read.page) / CGFloat(read.total)
                    lineView.backgroundColor = read.sound == "true" ? .systemBlue : .systemRed
                }else{
                    processConstraint.constant = 0
                }
            }
        }
    }
    var downloadAction = {}
    @IBAction func download(){
        downloadAction()
    }
    
}
